var timeoutid;
$(document).ready(function () {
    //找到标签
    /*  $("li").mouseover(function () {
    //隐藏原来现实的区域，显示当前对应的区域
    $("div.content1").hide();

    });*/
	$.selectType=1;
	$("#clearbtn").click(function(){
		/*自主考试*/
		$("#zj_englishscore").val("");
		$("#zj_majorscore").val("");
		$("#zj_totalscore").val("");
		/*推荐免试*/
		$("#tj_comprehensivetest").val("");
		/*考研调剂*/
		$("#ky_englishscore").val("");
		$("#ky_politicalscore").val("");
		$("#ky_majorscore1").val("");
		$("#ky_majorscore2").val("");
		/*面试*/
		$("#ms_englishscore").val("");
		$("#ms_professionalbasis").val("");
		$("#ms_researchability").val("");
		$("#ms_overallability").val("");
		$("#ms_writtenfacescore").val(""); 	
	});
	
	/*筛选*/
	$("#filterbtn").click(function(){
		$("#tag li").each(function(index){

				if($("#tag li").eq(index).hasClass("tagin")){
					$.selectType=index+1;
				 
				}
		});
//		switch ($.selectType){
//			case 1:alert("1");break;
//			case 2:
//			case 3:
//		}

		//筛选
			filterStudent($.selectType);
	});
	
	
	
	$(".inputtext").focus(function(){
					$(this).addClass("focustext");
								   });
		$(".inputtext").blur(function(){
					$(this).removeClass("focustext");
								   });
    $("#tag li").each(function (index) {
        $(this).mouseover(function () {
            var liNode = $(this);
            timeoutid = setTimeout(function () {
                $("#content div.content1").removeClass("content1");
                $("#tag li.tagin").removeClass("tagin");
                $("#content div").eq(index).addClass("content1");
                liNode.addClass("tagin");
            }, 300);
        }).mouseout(function () {
            clearTimeout(timeoutid);
        });
    });
//    $("#tj_enrolledstudentTable .enroll").click(function(){
//    	//var stuext=$("table#stuext_userName").val;
//    		alert("1");
//    	if(confirm("确认录取ID")){
//    		
//    	}
//    	
//    });
    
});

function soapWrapFunc(bdStr){
    // 封装SOAP头尾
    var soapHead = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/"><env:Body>', soapTail = '</env:Body></env:Envelope>';
    return soapHead + bdStr + soapTail;
}

function filterStudent(type){
	//验证报文
    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
	
	//面试成绩
	var stuext_englishResult=parseFloat($.trim($('#ms_englishscore').val())); //英语能力
	var stuext_majorResult=parseFloat($.trim($('#ms_professionalbasis').val())); //专业基础
	var stuext_practiseResult=parseFloat($.trim($('#ms_researchability').val())); //科研经历与实践能力
	var stuext_compreResult=parseFloat($.trim($('#ms_overallability').val())); //综合素质
	var stuext_sumscore=parseFloat($.trim($('#ms_writtenfacescore').val()));//面试笔试成绩
	
	if(isNaN(stuext_englishResult))
		stuext_englishResult=0;
	if(isNaN(stuext_majorResult))
		stuext_majorResult=0;
	if(isNaN(stuext_practiseResult))
		stuext_practiseResult=0;
	if(isNaN(stuext_compreResult))
		stuext_compreResult=0;
	if(isNaN(stuext_sumscore))
		stuext_sumscore=0;
	
	
	//自主考试
	if(type==1){
		var stuext_englishScore=parseFloat($.trim($('#zj_englishscore').val())); //英语成绩
		var stuext_majorScore=parseFloat($.trim($('#zj_majorscore').val()));//专业基础
		var totalscore=parseFloat($.trim($('#zj_totalscore').val()));//笔试总分
		
		if(isNaN(stuext_englishScore))
			stuext_englishScore=0;
		if(isNaN(stuext_majorScore))
			stuext_majorScore=0;
		if(isNaN(totalscore))
			totalscore=0;
		
		
		var sendBody='<web:FilterStudentExtendInfoType1>'+userinfo+
		'<stuext_englishScore>'+stuext_englishScore+'</stuext_englishScore>'+
		'<stuext_majorScore>'+stuext_majorScore+'</stuext_majorScore>'+
		'<totalscore>'+totalscore+'</totalscore>'+
		'<stuext_englishResult>'+stuext_englishResult+'</stuext_englishResult>'+
		'<stuext_majorResult>'+stuext_majorResult+'</stuext_majorResult>'+
		'<stuext_practiseResult>'+stuext_practiseResult+'</stuext_practiseResult>'+
		'<stuext_compreResult>'+stuext_compreResult+'</stuext_compreResult>'+
		'<stuext_sumscore>'+stuext_sumscore+'</stuext_sumscore>'+
		'</web:FilterStudentExtendInfoType1>';
		
	
		 $.ajax({
	            type: "POST",
	            contentType: "text/xml",
	            url: '../../Service/StudentExtendInfoService',
	            data: soapWrapFunc(sendBody),
	            dataType: 'xml',
	            success: function(xml){
	            	
	            	zj_displayData(xml);
	                
					// loadStudentData();
	            },
	            beforeSend: function(xhr){
	                xhr.setRequestHeader("SOAPAction", "\"\"");
	            },
	            error: function(x, e){
	            	var xmlObj = $(x.responseText);
	                if (xmlObj.find('faultstring').text() == 'oops') {
	                    alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
	                    return;
	                }
	                else {
	                    alert('error:' + x.responseText);
	                }
	            },
	            complete: function(x){
	            
	            }
	        });
		//推荐免试
	}else if(type==2){
		$("#zj_table").removeClass("showDiv").addClass("hideDiv");
		$("#tj_table").removeClass("hideDiv").addClass("showDiv");
		
		
		var stuext_complexScore=parseFloat($.trim($('#tj_comprehensivetest').val())); //综合测试
		
		if(isNaN(stuext_complexScore))
			stuext_complexScore=0;
		
		var sendBody='<web:FilterStudentExtendInfoType2>'+userinfo+
		'<stuext_complexScore>'+stuext_complexScore+'</stuext_complexScore>'+
		'<stuext_englishResult>'+stuext_englishResult+'</stuext_englishResult>'+
		'<stuext_majorResult>'+stuext_majorResult+'</stuext_majorResult>'+
		'<stuext_practiseResult>'+stuext_practiseResult+'</stuext_practiseResult>'+
		'<stuext_compreResult>'+stuext_compreResult+'</stuext_compreResult>'+
		'<stuext_sumscore>'+stuext_sumscore+'</stuext_sumscore>'+
		'</web:FilterStudentExtendInfoType2>';
		
		 $.ajax({
	            type: "POST",
	            contentType: "text/xml",
	            url: '../../Service/StudentExtendInfoService',
	            data: soapWrapFunc(sendBody),
	            dataType: 'xml',
	            success: function(xml){
	            	
	               
	                tj_displayData(xml);
	               
					// loadStudentData();
	            },
	            beforeSend: function(xhr){
	                xhr.setRequestHeader("SOAPAction", "\"\"");
	            },
	            error: function(x, e){
	            	var xmlObj = $(x.responseText);
	                if (xmlObj.find('faultstring').text() == 'oops') {
	                    alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
	                    return;
	                }
	                else {
	                    alert('error:' + x.responseText);
	                }
	            },
	            complete: function(x){
	            
	            }
	        });
		//考研调剂
	}else if(type==3){
		
	}else {
		System.out.println("系统参数错误");
	}
}

function zj_displayData(xml){

	var enrolledstudentTableHtml = '<tbody>';
	$.each($(xml).find("item"),function(i,n){	
		enrolledstudentTableHtml += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';

		enrolledstudentTableHtml += '<td>' + $(this).find('stuext_examCardID').text() +'<input type="hidden" id="stuext_id" value="' + $(this).find('stuext_id').text() + '" />'+ '</td>';
		enrolledstudentTableHtml += '<td id="stuext_userName">' + $(this).find('stuext_userName').text() + '</td>';
		enrolledstudentTableHtml += '<td>' + parseInt($(this).find('stuext_englishScore').text()) + '</td>';
		enrolledstudentTableHtml += '<td>' + parseInt($(this).find('stuext_majorScore').text()) + '</td>';
		enrolledstudentTableHtml += '<td>' + (parseInt($(this).find('stuext_englishScore').text())+parseInt($(this).find('stuext_majorScore').text())) + '</td>';
		enrolledstudentTableHtml += '<td>' + (parseInt($(this).find('stuext_englishResult').text())+parseInt($(this).find('stuext_majorResult').text())+parseInt($(this).find('stuext_practiseResult').text())+parseInt($(this).find('stuext_compreResult').text()))+ '</td>';
		enrolledstudentTableHtml += '<td>' + $(this).find('stuext_sumscore').text() + '</td>';
		enrolledstudentTableHtml += '<td><a class="enroll" href="javascript:void(0);" >录取</a><a class="cancel" href="javascript:void(0);" >取消</a></td>';
		
		enrolledstudentTableHtml += '</tr>';
	}	);
		enrolledstudentTableHtml += '</tbody>';
		 $('#enrolledstudentTable').find('tbody').remove().end().append(enrolledstudentTableHtml);

		 //自主 录取按钮
		 $("#enrolledstudentTable .enroll").click(function(){
		
			 var stuext_id= $(this).parents('tr').find('td').eq(0).find(':hidden').val();
			// var  stuext_userName= $(this).parents('tr').find('td').find('#stuext_userName').val();
			 
			 //alert("stuext_userName:"+stuext_userName);
			  enrollStudent(stuext_id);
			 
		 });
		 //自主 删除按钮
		 $("#enrolledstudentTable .cancel").click(function(){
			 var stuext_id= $(this).parents('tr').find('td').eq(0).find(':hidden').val();
			 
			 $.each($(xml).find("item"),function(i,n){	
				 if($(this).find('stuext_id').text()==stuext_id){
					 
					// $(xml).remove(this);
				 }
			 });
			 zj_displayData(xml);
		 });
}

function tj_displayData(xml){
	
	
	var tj_enrolledstudentTable = '<tbody>';
	$.each($(xml).find("item"),function(i,n){	
		tj_enrolledstudentTable += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
		
		tj_enrolledstudentTable += '<td>' + $(this).find('stuext_examCardID').text() +'<input type="hidden"  id="stuext_id" value="' + $(this).find('stuext_id').text() + '" />'+ '</td>';
		tj_enrolledstudentTable += '<td id="stuext_userName">' + $(this).find('stuext_userName').text() + '</td>';
		tj_enrolledstudentTable += '<td>' + parseInt($(this).find('stuext_complexScore').text()) + '</td>';
		tj_enrolledstudentTable += '<td>' + (parseInt($(this).find('stuext_englishResult').text())+parseInt($(this).find('stuext_majorResult').text())+parseInt($(this).find('stuext_practiseResult').text())+parseInt($(this).find('stuext_compreResult').text()))+ '</td>';
		tj_enrolledstudentTable += '<td>' + parseInt($(this).find('stuext_sumscore').text()) + '</td>';
		tj_enrolledstudentTable += '<td><a class="enroll" href="javascript:void(0);" >录取</a><a class="cancel" href="javascript:void(0);" >取消</a></td>';
		
		tj_enrolledstudentTable += '</tr>';
	});
	tj_enrolledstudentTable += '</tbody>';
	$('#tj_enrolledstudentTable').find('tbody').remove().end().append(tj_enrolledstudentTable);
	 //推荐 录取按钮
	$("#tj_enrolledstudentTable .enroll").click(function(){
		 var stuext_id=$(this).parents('tr').find('td').eq(0).find(':hidden').val();
		// var  stuext_userName=$.trim($('#stuext_userName').val());
		 
		  enrollStudent(stuext_id);
	 });
	//推荐 删除按钮
	$("#tj_enrolledstudentTable .cancel").click(function(){
		 
	 });
}


function enrollStudent(stuext_id){
	//验证报文
    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
    
	var sendBody='<web:EnrollStudent>'+userinfo+
	'<stuext_id>'+stuext_id+'</stuext_id>'+
	'</web:EnrollStudent>';
	//录取 	
	 $.ajax({
         type: "POST",
         contentType: "text/xml",
         url: '../../Service/StudentExtendInfoService',
         data: soapWrapFunc(sendBody),
         dataType: 'xml',
         success: function(xml){
         	if($.selectType==1){
         		alert($(xml).find('return').text()+stuext_id);

    			filterStudent($.selectType);
         	}else if($.selectType==2){
         		alert($(xml).find('return').text()+stuext_id);

    			filterStudent($.selectType);

         	}else{
         		//考研调剂
         	}
            
				// loadStudentData();
         },
         beforeSend: function(xhr){
             xhr.setRequestHeader("SOAPAction", "\"\"");
         },
         error: function(x, e){
        	 var xmlObj = $(x.responseText);
             if (xmlObj.find('faultstring').text() == 'oops') {
                 alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                 return;
             }
             else {
                 alert('error:' + x.responseText);
             }
         },
         complete: function(x){
         
         }
     });
	
}
